package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"video/api/request"
	"video/glodal"
	"video/model"
	"video/pkg"
)

func VideoList(c *gin.Context) {
	var req request.VideoList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	page := req.Page
	if page <= 0 {
		page = 1
	}

	pageSize := req.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	var video []model.Video
	glodal.DB.Offset(offset).Limit(req.Size).Find(&video)
	if req.Status != "" {
		glodal.DB.Where("status = ?", req.Status).Find(&video)
	}
	if req.Type != "" {
		glodal.DB.Where("type = ?", req.Type).Find(&video)
	}
	if req.Author != "" {
		glodal.DB.Where("author = ?", req.Author).Find(&video)
	}
	if req.Title != "" {
		glodal.DB.Where("title like ?", "%"+req.Title+"%").Find(&video)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
		"data": video,
	})
}

func VideoAdd(context *gin.Context) {
	var req request.VideoAdd
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	video := model.Video{
		Title:   req.Title,
		Image:   req.Image,
		Content: req.Content,
		Type:    req.Type,
		Status:  req.Status,
		Author:  req.Author,
		Address: req.Address,
	}

	glodal.DB.Create(&video)
	context.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
		"data": video.ID,
	})
}

func Upload(c *gin.Context) {
	var req request.Upload
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	file, m := c.FormFile("image")
	upload, err := pkg.Upload(file, m)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
		"data": upload,
	})
}

func Show(context *gin.Context) {
	var req request.VideoShow
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	var video model.Video
	glodal.DB.Where("id = ?", req.Id).First(&video)
	context.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
		"data": video,
	})
}

func VideoLook(context *gin.Context) {
	var req request.VideoNum
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	var video model.Download
	tx := glodal.DB.Begin()
	err := tx.Where("video_id=?", req.VideoId).First(&video)
	if err == nil {
		video.Num++
	}
	tx.Commit()
	context.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
		"data": video,
	})
}
